
import Foundation
import hpple

extension KMTool {
    
    
    //MARK: 修改状态栏颜色
    static func changeStatusColor(color:UIColor) {
        
        let statusBarWindow = UIApplication.shared.value(forKey: "statusBarWindow") as! UIView
        let statusBar = statusBarWindow.value(forKey: "statusBar") as! UIView
        if statusBar.responds(to: #selector(setter: UIView.backgroundColor)) {
            statusBar.backgroundColor = color
        }
    }
    
    //MARK: 判断是否为iPhoneX系列
    static func isNewiPhone() -> Bool {
        
        if Int(ScreenH/ScreenW * 100) == 216 {
            return true
        }
        
        return false
    }
    
    
    //MARK: 解析Html数据为数组
    fileprivate static func htmlToArray(data:Data,htmlElement:String,attributes:String) -> [String] {
        
        let hpple = TFHpple.init(htmlData: data)
        let liArray = hpple?.search(withXPathQuery: htmlElement) as NSArray?
        
        var elements = [String]()
        
        if attributes == "text" {
            for element in liArray! {
                let title = (element as? TFHppleElement)?.text()
                elements.append(title ?? "")
            }
        }else if attributes == "src" || attributes == "href" {
            for element in liArray! {
                let title = (element as? TFHppleElement)?.object(forKey: attributes)
                elements.append(title ?? "")
            }
        }else if attributes == "span" {
            for element in liArray! {
                let title = (element as? TFHppleElement)?.firstChild(withTagName: attributes).text()
                elements.append(title ?? "")
            }
        }
        
        
        
        return elements
    }
    
    //MARK: 解析Html数据为模型
    /// -data : 解析的html数据
    /// -elements : 解析的html元素
    /// -attributes : 解析的html元素的属性
    /// -keys : 字典键值
    static func htmlToModel(data:Data,elements:[String],attributes:[String],keys:[String]) -> [[String:String]] {
        
        var dicArray = [[String:String]]()
        var elementArray = [[String]]()
        
        for i in 0..<elements.count {
            
            let array = KMTool.htmlToArray(data: data, htmlElement: elements[i], attributes: attributes[i])
            elementArray.append(array)
        }
        
        for _ in 0..<elementArray[0].count {
            dicArray.append([String:String]())
        }
        
        for i in 0..<elementArray.count {
            
            let array = elementArray[i]
            for j in 0..<array.count {
                dicArray[j][keys[i]] = array[j]
            }
        }
        
        return dicArray
    }
    
    
    //MARK: 网络节点移除
    static func removeHtmlElement(classNames:[String]) -> String {
        
        var removeElement = ""
        
        for className in classNames {
            
            removeElement += "document.getElementsByClassName('\(className)')[0].remove();"
        }
        
        return removeElement
    }
    
    //MARK: 通过银行选照片
    static func selectBankImage(bankName:String) -> String {
        
        if bankName == "" {
            return ""
        }
        
        let moreBankArray = [["name":CITICBank,"image":"zhongxin"],["name":PufaBank,"image":"pufa"],["name":GuangfaBank,"image":"guangfa"],["name":EverbrightBank,"image":"guangda"],["name":PeaceBank,"image":"pingan"],["name":CitiBank,"image":"huaqi"],["name":GuangzhouBank,"image":"guangzhou"],["name":ZheshangBank,"image":"zheshang"],["name":ChinaMerchantsBank,"image":"zhaoshang"],["name":BankofCommunications,"image":"jiaotong"],["name":MinshengBank,"image":"minsheng"],["name":IndustrialBank,"image":"xingye"],["name":HSBCBank,"image":"huaxia"],["name":ShanghaiBank,"image":"shanghai"],["name":BankofChina,"image":"zhongguo"],["name":ABC,"image":"nongye"],["name":ICBC,"image":"gongshang"],["name":Constructionbank,"image":"jianshe"],["name":Postalsaving,"image":"youzheng"],["name":BaoshangBank,"image":"baoshang"],["name":BankofBeijing,"image":"beijing"],["name":ChongqingBank,"image":"chongqing"],["name":HSBC,"image":"huifeng"],["name":StandardCharteredBank,"image":"zhada"],["name":agriculturalBank,"image":""]]
        
        //索引
        var name = "yinlian"
        
        for model in moreBankArray {
            
            if (bankName as NSString).range(of: model["name"]!).location != NSNotFound {
                
                name = model["image"]!
                break
            }
        }
        
        return name
    }
    
    //MARK: 生成记录
    static func generateRecords(idString:String) {
        
        let recordId = idString.replacingOccurrences(of: "https://m.kameng98.com/mobile/details/i", with: "")
        
        if KMTool.isSignIn() {
            
            //用户记录
            KMTool.requestMethod(.post, URLString: url_record, parameters: ["user_name":KMTool.stringForEnumKey(.phone) ?? "","id":recordId], success: { (value, json) in
                
                if json["status"].stringValue == "0" {
                    
                }else{
                    KMTool.showText(_text: json["data"].stringValue)
                }
                
            }) {
                
            }
        }else{ //游客记录
            
            KMTool.requestData(URLString: url_touristsRecord, parameters: ["id":recordId,"device":"1","series":"0"], progress: nil) { (string) in
                
            }
        }
    }
    
    
}
